Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web
Imports DataTypes = KDESolutions.Articles.DataTypes

Public Class ArticlesService
    Private Shared _provider As ArticlesProvider = Nothing
    Private Shared _providers As ArticlesCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As ArticlesProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As ArticlesCollection
        Get
            Return _providers
        End Get
    End Property

    ' methods that work with categories
    Public Shared Function GetCategories() As List(Of DataTypes.CategoryDetails)
        LoadProviders()
        Return _provider.GetCategories()
    End Function
    Public Shared Function GetCategoryByID(ByVal categoryID As String) As DataTypes.CategoryDetails
        LoadProviders()
        Return _provider.GetCategoryByID(categoryID)
    End Function
    Public Shared Function DeleteCategory(ByVal categoryID As String) As Boolean
        LoadProviders()
        Return _provider.DeleteCategory(categoryID)
    End Function
    Public Shared Function UpdateCategory(ByVal category As DataTypes.CategoryDetails) As Boolean
        LoadProviders()
        Return _provider.UpdateCategory(category)
    End Function
    Public Shared Function InsertCategory(ByVal category As DataTypes.CategoryDetails) As String
        LoadProviders()
        Return _provider.InsertCategory(category)
    End Function

    ' methods that work with articles
    Public Shared Function GetArticles(ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.ArticleDetails)
        LoadProviders()
        Return _provider.GetArticles(pageIndex, pageSize)
    End Function
    Public Shared Function GetArticles(ByVal categoryID As String, ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.ArticleDetails)
        LoadProviders()
        Return _provider.GetArticles(categoryID, pageIndex, pageSize)
    End Function
    Public Shared Function GetArticleCount() As Integer
        LoadProviders()
        Return _provider.GetArticleCount
    End Function
    Public Shared Function GetArticleCount(ByVal categoryID As String) As Integer
        LoadProviders()
        Return _provider.GetArticleCount(categoryID)
    End Function
    Public Shared Function GetPublishedArticles(ByVal currentDate As DateTime, ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.ArticleDetails)
        LoadProviders()
        Return _provider.GetPublishedArticles(currentDate, pageIndex, pageSize)
    End Function
    Public Shared Function GetPublishedArticles(ByVal categoryID As String, ByVal currentDate As DateTime, ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.ArticleDetails)
        LoadProviders()
        Return _provider.GetPublishedArticles(categoryID, currentDate, pageIndex, pageSize)
    End Function
    Public Shared Function GetPublishedArticleCount(ByVal currentDate As DateTime) As Integer
        LoadProviders()
        Return _provider.GetPublishedArticleCount(currentDate)
    End Function
    Public Shared Function GetPublishedArticleCount(ByVal categoryID As String, ByVal currentDate As DateTime) As Integer
        LoadProviders()
        Return _provider.GetPublishedArticleCount(categoryID, currentDate)
    End Function
    Public Shared Function GetArticleByID(ByVal articleID As String) As DataTypes.ArticleDetails
        LoadProviders()
        Return _provider.GetArticleByID(articleID)
    End Function
    Public Shared Function DeleteArticle(ByVal articleID As String) As Boolean
        LoadProviders()
        Return _provider.DeleteArticle(articleID)
    End Function
    Public Shared Function UpdateArticle(ByVal article As DataTypes.ArticleDetails) As Boolean
        LoadProviders()
        Return _provider.UpdateArticle(article)
    End Function
    Public Shared Function InsertArticle(ByVal article As DataTypes.ArticleDetails) As String
        LoadProviders()
        Return _provider.InsertArticle(article)
    End Function
    Public Shared Function ApproveArticle(ByVal articleID As String) As Boolean
        LoadProviders()
        Return _provider.ApproveArticle(articleID)
    End Function
    Public Shared Function IncrementArticleViewCount(ByVal articleID As String) As Boolean
        LoadProviders()
        Return _provider.IncrementArticleViewCount(articleID)
    End Function
    Public Shared Function RateArticle(ByVal articleID As String, ByVal rating As Integer) As Boolean
        LoadProviders()
        Return _provider.RateArticle(articleID, rating)
    End Function
    Public Shared Function GetArticleBody(ByVal articleID As String) As String
        LoadProviders()
        Return _provider.GetArticleBody(articleID)
    End Function

    ' methods that work with comments
    Public Shared Function GetComments(ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.CommentDetails)
        LoadProviders()
        Return _provider.GetComments(pageIndex, pageSize)
    End Function
    Public Shared Function GetComments(ByVal articleID As String, ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.CommentDetails)
        LoadProviders()
        Return _provider.GetComments(articleID, pageIndex, pageSize)
    End Function
    Public Shared Function GetCommentCount() As Integer
        LoadProviders()
        Return _provider.GetCommentCount()
    End Function
    Public Shared Function GetCommentCount(ByVal articleID As String) As Integer
        LoadProviders()
        Return _provider.GetCommentCount(articleID)
    End Function
    Public Shared Function GetCommentByID(ByVal commentID As String) As DataTypes.CommentDetails
        LoadProviders()
        Return _provider.GetCommentByID(commentID)
    End Function
    Public Shared Function DeleteComment(ByVal commentID As String) As Boolean
        LoadProviders()
        Return _provider.DeleteComment(commentID)
    End Function
    Public Shared Function UpdateComment(ByVal comment As DataTypes.CommentDetails) As Boolean
        LoadProviders()
        Return _provider.UpdateComment(comment)
    End Function
    Public Shared Function InsertComment(ByVal comment As DataTypes.CommentDetails) As String
        LoadProviders()
        Return _provider.InsertComment(comment)
    End Function

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As ArticlesConfiguration = CType(ConfigurationManager.GetSection("ArticlesProvider"), ArticlesConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New ArticlesCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(ArticlesProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub
End Class
